require 'middleclass'
require 'Display'
require "Vector2D"
require 'MathUtils'

GravitySensor = class('GravitySensor', Display)

function GravitySensor:initialize(imageSheet,sheet,x, y, sizex, sizey, rotation)
    local sensor = display.newImageRect(imageSheet,sheet, sizex, sizey)
    sensor.x = x
    sensor.y = y
    sensor.rotation = rotation
    sensor.isVisible = debugMode
    physics.addBody( sensor,{ isSensor = true } )
    Display.initialize(self, sensor)
    self.gVector = MathUtils.rotateV({x=0,y=2.3},math.rad(rotation))
    self.rotation = rotation
    --collision listener
    self:addCollisionListener()
    print(self.gVector.x..':'..self.gVector.y)
end

function GravitySensor:collision(event)
    if ( event.other.cont.class.name == 'Player') then
        if ( event.phase == "began" ) then
            event.other.cont.pVector = self.gVector
            event.other.cont.pRotation = self.rotation
            event.other.cont.pCoef = event.other.cont.pCoef + 1
            print('began')
        elseif ( event.phase == "ended" ) then
            event.other.cont.pCoef = event.other.cont.pCoef - 1
            if(event.other.cont.pCoef ==0)then
                event.other.cont.pVector = nil
                event.other.cont.pRotation = nil
            end
            print('end')
        end
    end
end


